package cn.xp.service.impl;

import cn.xp.domain.Building;
import cn.xp.domain.Floor;
import cn.xp.dto.*;
import cn.xp.fegin.DeviceFeignClient;
import cn.xp.fegin.FloorFeignClient;
import cn.xp.mapper.BuildingMapper;
import cn.xp.service.IBuildingService;
import cn.xp.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BuildingService implements IBuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private FloorFeignClient floorFeignClient;
    @Autowired
    private DeviceFeignClient deviceFeignClient;
    @Override
    public Building findOne(Long id) {
        return buildingMapper.findOne(id);
    }

    /**
     * 批量新建楼层信息
     * @param buildings
     * @return
     */
    @Override
    public JSONResult<Building> addFloor(Building[] buildings) {

        for (Building building : buildings) {
            Floor floor = new Floor();
            buildingMapper.addFloor(building);
            floor.setBuildId(building.getId());
            floor.setName(building.getName());
            floorFeignClient.floorAdd(floor);

        }
        return JSONResult.success();
    }

    /**
     * 根据楼宇id查询设备
     * @param id
     * @return
     */
    @Override
    public JSONResult<BuilDDto> getDeviceListByBuildId(Long id) {
        Building building = buildingMapper.findOne(id);
        BuilDDto builDDto = new BuilDDto();
        builDDto.setBuildName(building.getBuildName());
        List<FloorDto> floorDtos = deviceFeignClient.floorList(id);
        builDDto.setFloorList(floorDtos);
        return JSONResult.success(builDDto);
    }

    @Override
    public JSONResult<BuilDTypeDto> getDeviceListByDeviceType(Long id) {
        Building building = buildingMapper.findOne(id);
        BuilDTypeDto builDTypeDto = new BuilDTypeDto();
        builDTypeDto.setBuildName(building.getBuildName());
        List<FloorDeviceDto> floorDeviceDtos = floorFeignClient.floorType(id);
        builDTypeDto.setFloorList(floorDeviceDtos);
        return JSONResult.success(builDTypeDto);
    }
}
